package com.abu.eduorder.controller;

import com.abu.eduorder.utils.AliPayTemplate;
import com.abu.eduorder.config.PayAsyncVo;
import com.abu.eduorder.config.PayParam;
import com.abu.eduorder.pojo.Order;
import com.abu.eduorder.pojo.PayLog;
import com.abu.eduorder.service.OrderService;
import com.abu.eduorder.service.PayLogService;
import com.abu.exceptionhandler.EduException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@Controller
@RequestMapping("/eduorder/order")
public class AliPayController {

    @Autowired
    private AliPayTemplate payTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private PayLogService payLogService;

    /**
     * 支付宝支付
     *
     * @return
     */
    @ResponseBody
    @GetMapping("alipay/{orderId}")
    public String alipay(@PathVariable("orderId") String orderId) {
//        根据订单编号查询订单金额
        Order order = this.orderService.queryOrderInfo(orderId);
        PayParam payParam = new PayParam();
        payParam.setOut_trade_no(orderId);        //订单编号不能重复
//        payParam.setTotal_amount(String.valueOf(new BigDecimal(12).longValue()));
        payParam.setTotal_amount(String.valueOf(order.getTotalFee().longValue()));
        payParam.setSubject("阿布小社");
        payParam.setBody("支付平台");
        String returnUrl = this.payTemplate.alipay(payParam, order.getCourseId());
        if (StringUtils.isBlank(returnUrl)) {
            return null;
        }
        return returnUrl;
    }

    /**
     * 支付宝回调
     *
     * @param payAsyncVo
     */

    @PostMapping("/ali/notify")
    public void notify(PayAsyncVo payAsyncVo) {
//         1.修改订单状态
        String orderNo = payAsyncVo.getOut_trade_no();
//        1.1根据订单号获修改订单状态为已支付
        UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
        wrapper.eq("order_no", orderNo);
        wrapper.set("status", 1);
        boolean flag = this.orderService.update(wrapper);
        if (!flag) {
            throw new EduException(20001, "交易失败");
        }
//         2.添加支付记录
//         2.1根据订单号获取订单信息
        Order order = this.orderService.getOne(new QueryWrapper<Order>().eq("order_no", orderNo));
        PayLog payLog = new PayLog();
        payLog
                .setOrderNo(orderNo)
                .setPayTime(new Date())
                .setTotalFee(order.getTotalFee())
                .setTradeState("已支付")
                .setAttr(null)
                .setTransactionId(payAsyncVo.getOut_trade_no())
                .setPayType(order.getPayType());
        boolean flagPayLog = this.payLogService.save(payLog);
        if (!flagPayLog) {
            throw new EduException(20001, "订单记录生成失败");
        }
//        String url = "http://localhost:3000/" + order.getCourseId();//指定URL
//        try {
//            HttpClientUtils.get(url);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

}
